#include <stdio.h>
#include <stdlib.h>
#include "bpt.h"
/* First message to the user.
 */
#define Version "1.4"

void usage_1( void ) {
  printf("B+ Tree of Order %d.\n", BPT_ORDER);
  printf("\tAmittai Aviram -- amittai.aviram@yale.edu  Version %s\n", Version);
  printf("\tfollowing Silberschatz, Korth, Sidarshan, Database Concepts, 5th ed.\n\n");
  printf("To build a B+ tree of a different order, start again and enter the order\n");
  printf("as an integer argument:  bpt <order>.  ");
  printf("3 <= order <=20\n");
  printf("To start with input from a file of newline-delimited integers, start again and enter\n");
  printf("the order followed by the filename:  bpt <order> <inputfile>.\n");
}

/* Second message to the user.
 */
void usage_2( void ) {
  printf("Enter any of the following commands after the prompt > :\n");
  printf("\ti <k>  -- Insert <k> (an integer) as both key and value).\n");
  printf("\tf <k>  -- Find the value under key <k>.\n");
  printf("\tp <k> -- Print the path from the root to key k and its associated value.\n");
  printf("\td <k>  -- Delete key <k> and its associated value.\n");
  printf("\tx -- Destroy the whole tree.  Start again with an empty tree of the same order.\n");
  printf("\tt -- Print the B+ tree.\n");
  printf("\tl -- Print the keys of the leaves (bottom row of the tree).\n");
  printf("\tq -- Quit. (Or use Ctl-D.)\n");
  printf("\t? -- Print this help message.\n");
}

// MAIN

int main( int argc, char ** argv ) {

	char * input_file;
	FILE * fp;
	node * root;
	record * r;
	int input;
	char instruction;

	root = NULL;

	if (BPT_ORDER < 3 || BPT_ORDER > 20)
		exit(EXIT_FAILURE);

  usage_1();  
  usage_2();

  if (argc > 2) {
    input_file = argv[2];
    fp = fopen(input_file, "r");
    if (fp == NULL) {
      perror("Failure to open input file.");
      exit(EXIT_FAILURE);
    }
    while (!feof(fp)) {
      fscanf(fp, "%d\n", &input);
      root = insert(root, input, input);
    }
    fclose(fp);
    print_tree(root);
  }

  printf("> ");
  while (scanf("%c", &instruction) != EOF) {
    switch (instruction) {
    case 'i':
      scanf("%d", &input);
      while (getchar() != (int)'\n');
      root = insert(root, input, input);
      print_tree(root);
      break;
    case 'f':
    case 'p':
      scanf("%d", &input);
      while (getchar() != (int)'\n');
      r = find(root, input);
      if (r == NULL)
	printf("Record not found under key %d.\n", input);
      else 
	printf("Record at %x -- key %d, value %d.\n", (unsigned int)r, input, r->value);
      break;
    case 'd':
      scanf("%d", &input);
      while (getchar() != (int)'\n');
      root = delete(root, input);
      print_tree(root);
      break;
    case 'x':
      while (getchar() != (int)'\n');
      root = destroy_tree(root);
      print_tree(root);
      break;
    case 'l':
      while (getchar() != (int)'\n');
      print_leaves(root);
      break;
    case 't':
      while (getchar() != (int)'\n');
      print_tree(root);
      break;
    case 'q':
      while (getchar() != (int)'\n');
      return EXIT_SUCCESS;
    default:
      usage_2();
      break;
    }
    printf("> ");
  }
  printf("\n");
  
  return EXIT_SUCCESS;
}
